package com.widget;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class ListMsgHandler extends Handler{
//	private static final int APK_SYNCED=1000;
	static final int SYNCED=1000;
	static final int UNREACHABLE=1001;
	static final int REFUSED=1002;
	static final int TIMEOUT=1003;
	static final int ERROR=1004;
	static final int ACQUIRED=1005;
	static final int QUIT=1006;
	
	private Context mContext;
	private ProgressDialog d;
	private AlertDialog.Builder alertDialog;
	ManageProcess process;
	String []components;
	ListParser parser;
	private int uid;
	public ListMsgHandler(Context c){
		mContext = c;
		alertDialog = new AlertDialog.Builder(mContext);
	}
	public void setDialog(ProgressDialog dialog){
		d = dialog;
	}
	public void setDataItems(ManageProcess p, String[] c,int u){
		process = p;
		components = c;
		uid = u;
	}
	public void setListParser(ListParser p){
		parser = p;
	}
	@Override
      public void handleMessage(Message msg) {
      	
		if(msg.what == QUIT) {
			d.dismiss();
		}
		if(msg.what == ACQUIRED){
			ListAppsActivity.changePermissions(process, uid, parser.getDataFiles(),this);
		}
      	if(msg.what == SYNCED)
      	{
      		ListAppsActivity.acquireFiles(process, components, this);
      	}
      	if(msg.what == UNREACHABLE)
      	{
      		d.dismiss();
      		alertDialog.setMessage("Network is Unreachable!!!. Please make sure you are connected to Internet.");
      	    alertDialog.setTitle("Error");
      	    alertDialog.show();
      	}
      	if(msg.what == REFUSED)
      	{
      		d.dismiss();
      		alertDialog.setMessage("Connection Refused!!!. Server might be down.");
      	    alertDialog.setTitle("Error");
      	    alertDialog.show();
      	}
      	if(msg.what == TIMEOUT)
      	{
      		d.dismiss();
      		alertDialog.setMessage("Connection Timed Out!!!. Please make sure IP is correct.");
      	    alertDialog.setTitle("Error");
      	    alertDialog.show();
      	}
      	if(msg.what == ERROR)
      	{
      		d.dismiss();
      		/*
      		alertDialog.setMessage("Sync was not successfull!!!");
      	    alertDialog.setTitle("Error");
      	    alertDialog.show();
      	    */
      	}
      
      
	}

}
